package org.esgi.model.impl;

import java.util.Arrays;

import org.esgi.model.interfaces.IUser;

public class User implements IUser
{
	private String login_;
	private String pwd_;
	private Byte[] rules_;

	public User() 
	{
		rules_ = new Byte[4];
	}
	
	public User(String login)
	{
		this();
		this.setLogin(login);
	}
	
	public User(String login, String password)
	{
		this(login);
		this.setPassword(password);
	}
	
	@Override
	public String getLogin() 
	{
		return this.login_;
	}

	@Override
	public void setLogin(String login) 
	{
		this.login_ = login;
	}

	@Override
	public String getPassword()
	{
		return this.pwd_;	
	}

	@Override
	public void setPassword(String password)
	{
		this.pwd_ = password;
	}

	@Override
	public boolean isSuper() 
	{
		Byte b = this.rules_[0];
		if(b != null && b == 1)
			return true;
		return false;
	}

	@Override
	public void setIsSuper(boolean value) 
	{
		if(value == true)
			this.rules_[0] = 1;
		else
			this.rules_[0] = 0;
	}

	@Override
	public boolean canCreate()
	{
		Byte b = this.rules_[1];
		if(b != null && b == 1)
			return true;
		return false;
	}

	@Override
	public void setCanCreate(boolean value) 
	{
		if(value == true)
			this.rules_[1] = 1;
		else
			this.rules_[1] = 0;	
	}

	@Override
	public boolean canModify() 
	{
		Byte b = this.rules_[2];
		if(b != null && b == 1)
			return true;
		return false;
	}

	@Override
	public void setCanModify(boolean value)
	{
		if(value == true)
			this.rules_[2] = 1;
		else
			this.rules_[2] = 0;
	}

	@Override
	public boolean canRead() 
	{
		Byte b = this.rules_[3];
		if(b != null && b == 1)
			return true;
		return false;
	}

	@Override
	public void setCanRead(boolean value) 
	{
		if(value == true)
			this.rules_[3] = 1;
		else
			this.rules_[3] = 0;
	}
	
	public String getStringRules()
	{
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<rules_.length; i++)
		{
			Byte b = rules_[i];
			sb.append((b!=null)?b:0);
		}
		return sb.toString();
	}
	
	@Override
	public String toString() {
		return "User [login_=" + login_ + ", pwd_=" + pwd_ + ", rules_="
				+ Arrays.toString(rules_) + "]";
	}

	@Override
	public int hashCode() 
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + ((login_ == null) ? 0 : login_.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) 
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (login_ == null) {
			if (other.login_ != null)
				return false;
		} else if (!login_.equals(other.login_))
			return false;
		return true;
	}	
}
